package com.handinfo.android.core.resource;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.R.color;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PointF;
import android.view.MotionEvent;

import com.handinfo.android.DWGameActivity;
import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.input.DWKey;
import com.handinfo.android.core.media.DWAudio;
import com.handinfo.android.core.net.BaseConnection;
import com.handinfo.android.core.net.DWConnectionCreator;
import com.handinfo.android.core.net.GameProtocol;
import com.handinfo.android.core.net.HttpConnect;
import com.handinfo.android.core.object.AbstractGameHandler;
import com.handinfo.android.download.DialogHelper;
import com.handinfo.android.download.Update;
import com.handinfo.android.download.UpdateManager;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.Loading;
import com.handinfo.android.game.Login;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
//import com.handinfo.android.utils.ChannelOperation;
import com.handinfo.android.utils.DWChannel;
import com.handinfo.android.utils.OChannelOperation;
import com.handinfo.android.utils.Tools;

public class ResourceVerifier extends AbstractGameHandler
{
	public String updataVersions = null; // 更新后的版本
	public boolean isSendMissRes = false; // 是否发送了缺失资源
	public final static String CLIENT_VERSION ="1.0.0.0"; //"1.0.2.3";
	public final static String RESOURCE_VERSION = "1.0.0";//"16";
	public static boolean ISCODE_PACKAGE = false; // true :代码包
	
	public static boolean ISHALF_PACKAGE = false;// 完整资源包
	//#if full_res_version
//	public static boolean ISHALF_PACKAGE = false;// 完整资源包
	//#else
	//# public static boolean ISHALF_PACKAGE = true;// 半资源包
	//#endif

	public byte m_state = m_selictSer;

	private static final byte m_selictSer = 0;
	public static final byte LOGIC_Logo = 1;
	public static final byte LOGIC_Logo2 = 2;
	public static final byte LOGIC_ISFIRSTLOGIN = 3; // 判断是否第一次安装
	public static final byte LOGIC_ISFIRSTLOGIN_DEALING = 4; // 第一次安装处理数据
	public static final byte LOGIC_ISFIRSTLOGINCC = 5; // 第一次安装结束标记
	public static final byte LOGIC_Waite_Connection = 6; // 等待联网
	public static final byte LOGIC_ISHTTP = 7; // http
	public static final byte LOGIC_Connection = 8; // 连接网络结束
	public static final byte LOGIC_ISCodeVersion = 9; // 检查客户端版本
	public static final byte LOGIC_ISCodeVersion_Waite_Check = 10; // 等待服务器返回客户端版本
	public static final byte LOGIC_ISCodeVersion_Waite_Download = 11; // 下载客户端中
	public static final byte LOGIC_ISResourceVersion = 12; // 检查资源版本
	public static final byte LOGIC_ISResourceVersion_Download = 13; // 下载资源中
	public static final byte LOGIC_IS_INITSDK = 14; //是否初始化好sdk
	public static final byte LOGIC_LOGIN = 15; // 进入login
	public static final byte LOGIC_Waite = 100; // 等待状态
	// ////////////////////////////////////////////////////////////////////

	Bitmap m_logo_img;
	Bitmap m_login_img;
	Bitmap m_login_img_max;
	long m_login_time = 0;

	public void setState(byte currentState)
	{
		m_state = currentState;
	}

	/**
	 * 连接http
	 */
	public void stateHttpLJ()
	{
		m_state = LOGIC_ISHTTP;
		initHttp();
	}

	public void logic()
	{
		switch (m_state) {
			case m_selictSer:
				break;
			case LOGIC_Logo:
				//注释
//				ChannelOperation.initChannel();
				m_login_time = System.currentTimeMillis();
				m_state = LOGIC_Logo2;
				break;
			case LOGIC_Logo2:
				if ((System.currentTimeMillis() - m_login_time) > 2000) {
					m_state = LOGIC_ISFIRSTLOGIN;
				}
				break;
			case LOGIC_ISFIRSTLOGIN:
				if (DWGameManager.s_firstLogin) { // 第一次启动游戏才进
					m_state = LOGIC_ISFIRSTLOGIN_DEALING;
					if (!ISCODE_PACKAGE) {
						DWGameManager.s_config
								.edit()
								.putString(DWGameManager.CFG_KEY_RES_VERSION,
										RESOURCE_VERSION).commit();
						assetsToSDcard();
					}
					else {
						m_state = LOGIC_ISFIRSTLOGINCC;
					}
				}
				else {
					m_state = LOGIC_ISFIRSTLOGINCC;
				}
				break;
			case LOGIC_ISFIRSTLOGIN_DEALING:
				// 处理等待中,无操作
				break;
			case LOGIC_ISFIRSTLOGINCC:
				DWAudio.getInstance().loadMusic(DWAudio.m_主界面音乐);
				DWAudio.getInstance().playMusic();
				for (byte i = 0; i < DWAudio.mSOUND_NUM; i++) {
					DWAudio.getInstance().loadSoundPool(i);
				}
				DWControlsManager.createDWControlsManager();
				stateHttpLJ();
				break;
			case LOGIC_ISHTTP:
				break;
			case LOGIC_Waite_Connection:
				setState(LOGIC_Connection);
				openConnection();
				break;
			case LOGIC_Connection:
				if (m_connectionCreator != null) {
					if (m_connectionCreator.getState() == DWConnectionCreator.SUCCESS) {
						//////////////////////////////
						//Yujiang 跳过检测
//						setState(LOGIC_ISCodeVersion);				
						endlogo();
						//////////////////////////////
						m_connectionCreator = null;
					}
					else if (m_connectionCreator.getState() == DWConnectionCreator.FAILED) {
						DWGameManager.dealForConnectionException("连接失败", "无法连接到服务器,请检查网络设置以及状态!", "重试", "退出游戏");
						m_connectionCreator = null;
					}
					else if (m_connectionCreator.getLiveTime() > DWConnectionCreator.TIME_OUT) {
						DWGameManager.dealForConnectionException("连接超时", "连接到服务器超时,请检查网络设置以及状态!", "重试", "退出游戏");
						m_connectionCreator = null;
					}
				}
				break;
			case LOGIC_ISCodeVersion:
				//注释
//				OChannelOperation.checkVersion();
				m_state = LOGIC_ISCodeVersion_Waite_Check;
				break;
			case LOGIC_ISCodeVersion_Waite_Check:
				// 处理等待中,无操作
				break;
			case LOGIC_ISCodeVersion_Waite_Download:
				// 处理等待中,无操作
				break;
			case LOGIC_ISResourceVersion:
				byte value = 0;//0是不需要HD高清UI 1是需要
				byte halfRes = 0;//0是不需要请求 1是需要请求 ： 半资源
				//如果是半资源包 并且没有下载过补充资源
				if (ISHALF_PACKAGE && !DWGameManager.Half_res) {
					halfRes = 1;
				}
				//如果是完整包，并且是没有适配UI的高清用户，需要更新单独的高清资源
				if (DWGameManager.AdpaterType == DWGameManager.AD_HIGH
						&& DWGameManager.AdpaterValue != 1) {
					value = 1;
				}
				String resVersions = DWGameManager.s_config.getString(
						DWGameManager.CFG_KEY_RES_VERSION, RESOURCE_VERSION);
				DWGameManager.getInstance().getSendMessage()
						.sendCurrentResourceVersions(resVersions, (byte) DWGameManager.AdpaterType, value, halfRes);
				m_state = LOGIC_ISResourceVersion_Download;
				break;
			case LOGIC_ISResourceVersion_Download:
				// 处理等待中,无操作
				break;
			case LOGIC_IS_INITSDK:
				//注释
//				if (ChannelOperation.isInitSdk) {
					m_state = LOGIC_LOGIN;
//				}
				break;
			case LOGIC_LOGIN:
				Login login = new Login();
				DWGameManager.getInstance().setGameHandler(login);
				DWGameManager.getInstance().setGameState(DWGameManager.GS_LOGIN);
				Tools.debugPrintln("游戏主状态：" + DWGameManager.getInstance().getGameState());
				++m_state;
				break;
		}
		if (DWControlsManager.getInstance() != null) {
			DWControlsManager.getInstance().logic();
		}
	}

	/**
	 * 第一次安装做资源映射
	 */
	private void ressFirstInstallMapping(byte index, String dir)
	{
		File f = new File(dir);
		String[] files = f.list();
		if (files == null)
			return;
		for (int i = 0; i < files.length; i++) {
			String fileName = files[i];
			if (!fileName.contains(".")) {
				ressFirstInstallMapping(index, dir + fileName + "/");
				continue;
			}
			String assetPath = dir + fileName;
			int a = assetPath.indexOf("/", index + 1);
			int b = assetPath.lastIndexOf(".");
			ResHead resHead = new ResHead();
			String resURL = assetPath.substring(a + 1);
			resHead.m_url = resURL;
			if (assetPath.substring(b).equals(
					DWSerializableFactory.EXTENSION_ANIM)) {
				resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_ANIM;
				ressFirstInstall.put(resHead.m_url, resHead);
			}
			else if (assetPath.substring(b).equals(
					DWSerializableFactory.EXTENSION_BG)) {
				resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_BG;
				ressFirstInstall.put(resHead.m_url, resHead);
			}
			else if (assetPath.substring(b).equals(
					DWSerializableFactory.EXTENSION_IMG)) {
				resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_PNG;
				ressFirstInstall.put(resHead.m_url, resHead);
			}
			else if (assetPath.substring(b).equals(
					DWSerializableFactory.EXTENSION_SCENE)) {
				resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_SCENE;
				ressFirstInstall.put(resHead.m_url, resHead);
			}
			else if (assetPath.substring(b).equals(
					DWSerializableFactory.EXTENSION_SOUND)) {
				resHead.m_type = DWSerializableFactory.RESOURCE_TYPE_SOUND;
				ressFirstInstall.put(resHead.m_url, resHead);
			}
			// Tools.debugPrintln(resHead.m_url);
		}
	}

	Map<String, ResHead> ressFirstInstall = new HashMap<String, ResHead>();
	private static final String ASSETS_PATH = "res";
	// private String sdCardPath = "mnt/sdcard/xianyuanonline/res/"; //29
	// private String dataPath =
	// "data/data/com.handinfo.android/files/res/";//40
	private static final String RES_ZIP_NAME = "ress.zip";

	// /**
	// * 转移资源
	// * 0 SD卡 1 机身
	// */
	// public void assetsToSDcard(final byte type)
	// {
	// new Thread(new Runnable() {
	// public void run()
	// {
	// long time1 = System.currentTimeMillis();
	// boolean isUnzip = false;
	// try {
	// File dalateFile = null;
	// if (type == 0) {
	// CopyAssets(assetsPath, sdCardPath);
	// isUnzip = IORWFile.unZip(sdCardPath + zipPath, sdCardPath);
	// dalateFile = new File(sdCardPath + zipPath);
	//
	// }
	// else if (type == 1) {
	// CopyAssets(assetsPath, dataPath);
	// isUnzip = IORWFile.unZip(dataPath + zipPath, dataPath);
	// dalateFile = new File(sdCardPath + zipPath);
	// }
	// if (dalateFile != null && dalateFile.exists()) {
	// dalateFile.delete();
	// }
	// }
	// catch (Exception e) {
	// Tools.debug(e);
	// }
	// long time2 = System.currentTimeMillis() - time1;
	// Tools.debugPrintln("转移资源+解压资源所用时间：：：：  " + time2 / 1000 + "秒");
	// // if (isUnzip) {
	// // if (type == 0) {
	// // ressFirstInstallMapping((byte) 29, sdCardPath);
	// // }
	// // else if (type == 1) {
	// // ressFirstInstallMapping((byte) 40, dataPath);
	// // }
	// // IORWFile.XmlFileCreator(ressFirstInstall);
	// // }
	// m_state = LOGIC_ISFIRSTLOGINCC;
	// }
	// }).start();
	// }

	/**
	 * 转移资源
	 */
	public void assetsToSDcard()
	{
		new Thread(new Runnable() {
			public void run()
			{
				android.os.Process
						.setThreadPriority(android.os.Process.THREAD_PRIORITY_DISPLAY);
				long time1 = System.currentTimeMillis();
				try {
					File deleteFile = null;
					CopyAssets(ASSETS_PATH, DWGameManager.s_res_dir
							+ File.separator);
					IORWFile.unZip(DWGameManager.s_res_dir + File.separator
							+ RES_ZIP_NAME, DWGameManager.s_res_dir
							+ File.separator);
					deleteFile = new File(DWGameManager.s_res_dir
							+ File.separator + RES_ZIP_NAME);
					if (deleteFile != null && deleteFile.exists()) {
						deleteFile.delete();
					}
					long time2 = System.currentTimeMillis() - time1;
					Tools.debugPrintln("转移资源+解压资源所用时间：：：：  " + time2 / 1000
							+ "秒");
					// if (isUnzip) {
					// if (type == 0) {
					// ressFirstInstallMapping((byte) 29, sdCardPath);
					// }
					// else if (type == 1) {
					// ressFirstInstallMapping((byte) 40, dataPath);
					// }
					// IORWFile.XmlFileCreator(ressFirstInstall);
					// }
					DWGameManager.s_config.edit().putBoolean(DWGameManager.CFG_KEY_FIRST_LOGIN,
							false).commit();
					setState(LOGIC_ISFIRSTLOGINCC);
				}
				catch (FileNotFoundException e2) {
					//注释
//					ChannelOperation.assetsToSDcardError();
					DWGameManager.s_config.edit().putBoolean(DWGameManager.CFG_KEY_FIRST_LOGIN,
							true).commit();
				}
				catch (Exception e) {
					Tools.debug(e);
					DWGameManager.s_config.edit().putBoolean(DWGameManager.CFG_KEY_FIRST_LOGIN,
							true).commit();
				}
			}
		}).start();
	}

	/**
	 * 转移asserts到sd卡(或机身)
	 *
	 * @param assetDir
	 * @param dir
	 * @throws IOException
	 */
	private void CopyAssets(String assetDir, String dir) throws IOException
	{
		String[] files = DWGameManager.getInstance().getCurrentActivity()
				.getResources().getAssets().list(assetDir);
		File mWorkingPath = new File(dir);

		// if this directory does not exists, make one.
		if (!mWorkingPath.exists()) {
			if (!mWorkingPath.mkdirs()) {

			}
		}

		for (int i = 0; i < files.length; i++) {
			String fileName = files[i];
			// we make sure file name not contains '.' to be a folder.
			if (!fileName.equals("ress.zip")) {
				continue;
			}
			File outFile = new File(mWorkingPath, fileName);
			if (outFile.exists()) {
				outFile.delete();
			}

			InputStream in = null;
			if (0 != assetDir.length())
				in = DWGameManager.getInstance().getCurrentActivity()
						.getAssets().open(assetDir + "/" + fileName);
			else
				in = DWGameManager.getInstance().getCurrentActivity()
						.getAssets().open(fileName);
			FileOutputStream out = new FileOutputStream(outFile);
			// Transfer bytes from in to out
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			// ressFirstInstall
			out.flush();
			in.close();
			out.close();
			break;
		}
	}

//	/**
//	 * 转移asserts到sd卡(或机身)
//	 *
//	 * @param assetDir
//	 * @param dir
//	 * @throws IOException
//	 */
//	private void CopyAssets(String assetDir, String dir) throws IOException
//	{
//		String[] files = DWGameManager.getInstance().getCurrentActivity()
//				.getResources().getAssets().list(assetDir);
//		File mWorkingPath = new File(dir);
//
//		// if this directory does not exists, make one.
//		if (!mWorkingPath.exists()) {
//			if (!mWorkingPath.mkdirs()) {
//
//			}
//		}
//
//		for (int i = 0; i < files.length; i++) {
//			String fileName = files[i];
//			// we make sure file name not contains '.' to be a folder.
//			if (!fileName.contains(".")) {
//				CopyAssets(assetDir + "/" + fileName, dir + fileName + "/");
//				continue;
//			}
//			File outFile = new File(mWorkingPath, fileName);
//			if (outFile.exists()) {
//				outFile.delete();
//			}
//
//			InputStream in = null;
//			if (0 != assetDir.length())
//				in = DWGameManager.getInstance().getCurrentActivity()
//						.getAssets().open(assetDir + "/" + fileName);
//			else
//				in = DWGameManager.getInstance().getCurrentActivity()
//						.getAssets().open(fileName);
//			FileOutputStream out = new FileOutputStream(outFile);
//			// Transfer bytes from in to out
//			byte[] buf = new byte[1024];
//			int len;
//			while ((len = in.read(buf)) > 0) {
//				out.write(buf, 0, len);
//			}
//			// ressFirstInstall
//			out.flush();
//			in.close();
//			out.close();
//		}
//	}

	public boolean recvMessage(int type, DataInputStream dis) throws Exception
	{
		int order = BaseConnection.s_perseOrderID;
		Tools.debugPrintln("logo 接收到指令号 : " + order);
		DWGameManager.getInstance().getSendMessage()
				.unlockNetwork(GameMessage.锁_默认);
		try {
			switch (order) {
				case GameProtocol.SC_RES_CMD_CHECK_VERSION:
					recvServerVersions(dis);
					break;
				case GameProtocol.SC_REQUEST_VERSIONS_OVER:
					recvUpdataResEnd(dis);
					break;
				case GameProtocol.SC_RECEIVE_RESOURCE:
					recvResource(dis);
					break;
				case GameProtocol.SC_CHECK_VERSION:
					recvCheckVersion(dis);
					break;
				case GameProtocol.S_RES_ZIP_UPDATA:
//					recvRESZip(dis);
					break;
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
		return false;
	}

	Map<String, ResHead> missRes;
	public static Map<String, ResHead> resAll = null; // 本地所有的资料集合
	public ArrayList<String> m_resZip_key;

//	private int zipIndex = 0;

	/**
	 * 获得服务器的版本号
	 *
	 * @param body
	 */
	public void recvServerVersions(final DataInputStream dis)
	{
		try {
			Tools.debugPrintln("新版本资源资源："
					+ GameProtocol.SC_RES_CMD_CHECK_VERSION);
			updataVersions = dis.readUTF(); // 更新后的版本
			final int resNum = dis.readInt(); // 资源数
			Tools.debugPrintln("zip资源数 ：　" + resNum);

			if (resNum == 0) {
				endlogo();
			} else {
				//#if channel_id == 1201
				//#	DWGameManager.getInstance().getCurrentActivity().mHandler
				//#			.post(new Runnable() {
				//#				public void run() {
				//#					// TODO Auto-generated method stub
				//#					DialogHelper
				//#							.Confirm(
				//#								DWGameManager
				//#											.getInstance()
				//#											.getCurrentActivity(),
				//#									"提示",
				//#									"您需要更新数据才能继续进行游戏，点击“确认”开始更新，点击“退出”退出游戏。",
				//#									"确定",
				//#									new DialogInterface.OnClickListener()
				//#
				//#									{
				//#
				//#										public void onClick(
				//#												DialogInterface dialog,
				//#												int which) {
				//#											m_zip_path = DWGameManager.s_res_dir
				//#													+ File.separator;
				//#											m_resZip_key = new ArrayList<String>();
				//#											for (int i = 0; i < resNum; i++) {
				//#												String key;
				//#												try {
				//#													key = dis
				//#															.readUTF();
				//#													Tools.debugPrintln("zip key ="
				//#															+ key);
				//#													m_resZip_key
				//#															.add(key); // 资源key
				//#													File dalate = new File(
				//#															m_zip_path
				//#																	+ key);
				//#													if (dalate != null
				//#															&& dalate
				//#																	.exists()) {
				//#														dalate.delete();
				//#													}
				//#													// String
				//#													// rKey =
				//#													// m_resZip_key.get(0);
				//#													// DWGameManager.getInstance().getSendMessage()
				//#													// .sendResourceRequestZip(rKey,
				//#													// 0);
				//#													httpDownZip();
				//#													unZipOn();
				//#												} catch (IOException e) {
				//#													// TODO
				//#													// Auto-generated
				//#													// catch
				//#													// block
				//#													e.printStackTrace();
				//#												}
				//#											}
				//#										}
				//#									},
				//#									"退出",
				//#									new DialogInterface.OnClickListener() {
				//#
				//#										public void onClick(
				//#												DialogInterface dialog,
				//#												int which) {
				//#											DWGameManager.getInstance().destroy();
				//#
				//#										}
				//#									});
				//#				}
				//#			});
				//#else
					 m_zip_path = DWGameManager.s_res_dir + File.separator;
					 m_resZip_key = new ArrayList<String>();
					 for (int i = 0; i < resNum; i++) {
						String key = dis.readUTF();
					    Tools.debugPrintln("zip key =" + key);
						m_resZip_key.add(key); // 资源key
						File dalate = new File(m_zip_path + key);
						if (dalate != null && dalate.exists()) {
							dalate.delete();
						}
					 }
					 // String rKey = m_resZip_key.get(0);
					 // DWGameManager.getInstance().getSendMessage()
					 // .sendResourceRequestZip(rKey, 0);
					 httpDownZip();
					 unZipOn();
				//#endif

			}
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}
	}

	private int m_httpDownZip_index = 0;
	public boolean m_isHttpDown_now = false; //是否正在下载zip

	public synchronized void httpDownZipstate()
	{
		if (m_httpDownZip_index < m_resZip_key.size()) {
			zipUnList.add(m_resZip_key.get(m_httpDownZip_index));
		}
		m_httpDownZip_index++;
		m_isHttpDown_now = false;
		Tools.debugPrintln("m_httpDownZip_index " + m_httpDownZip_index);
	}

	private void httpDownZip()
	{
		new Thread(new Runnable() {

			public void run()
			{
				try {
					int size = m_resZip_key.size();
					while (m_httpDownZip_index < size) {
						if (!m_isHttpDown_now) {
							m_isHttpDown_now = true;
							DWGameManager.getInstance().getCurrentActivity().mHandler.post(new Runnable() {

								public void run()
								{
									Tools.debugPrintln(" http  index: " + m_httpDownZip_index);
									String url = m_resZip_key.get(m_httpDownZip_index);
									String zipName = url.substring(url.lastIndexOf("/") + 1);
									Tools.debugPrintln("zipname http   " + zipName);
									Update up = new Update(DWGameManager.getInstance().getCurrentActivity(), url,
											m_zip_path, zipName);
								}
							});
						}
						else {
							Thread.sleep(100);
						}
					}
				}
				catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

//	public long zip_index = 0;
//	public int m_100K = 102400;
//	public int m_pos = 0;
//	int currentZipsize = 0;
//	byte[] resdata;
	public String m_zip_path;

//	private void recvRESZip(DataInputStream dis)
//	{
//		try {
//			if (resAll == null) {
//				resAll = new HashMap<String, ResHead>();
//			}
//			String key = dis.readUTF(); // 文件key
//			currentZipsize = dis.readInt(); // zip文件的大小
//			Tools.debugPrintln("接收zip");
//			File file = new File(m_zip_path);
//			if (!file.exists()) {
//				file.mkdirs();
//			}
//			RandomAccessFile oSavedFile = new RandomAccessFile(
//					m_zip_path + key, "rw");
//			// 定位文件指针到nPos位置
//			oSavedFile.seek(m_pos);
//			int dataSize = dis.readInt();
//			byte[] b = new byte[dataSize];
//			dis.read(b);
//			oSavedFile.write(b);
//			m_pos = m_pos + dataSize;
//			if (m_pos < currentZipsize) {
//				DWGameManager.getInstance().getSendMessage()
//						.sendResourceRequestZip(key, m_pos);
//			}
//			else {
//				zipUnList.add(m_resZip_key.get(zipIndex));
//				zipIndex++;
//				m_pos = 0;
//				if (m_resZip_key.size() > zipIndex) {
//					String rKey = m_resZip_key.get(zipIndex);
//					DWGameManager.getInstance().getSendMessage()
//							.sendResourceRequestZip(rKey, 0);
//				}
//				else {
//					// int ksize = m_resZip_key.size();
//					// File dalateFile;
//					// for (int i = 0; i < ksize; i++) {
//					// String ekey = m_resZip_key.get(i);
//					// IORWFile.unZip(m_zip_path + ekey, m_zip_path);
//					// dalateFile = new File(m_zip_path + ekey);
//					// if (dalateFile != null && dalateFile.exists()) {
//					// dalateFile.delete();
//					// }
//					// }
//					// int reswhile =
//					// DWGameManager.getInstance().handinfoShared.getInt(IORWFile.sharedxmlResWhileKeep,
//					// 0);
//					// if (reswhile == 0) {
//					// ressFirstInstallMapping((byte) 29, sdCardPath);
//					// }
//					// else if (reswhile == 1) {
//					// ressFirstInstallMapping((byte) 40, dataPath);
//					// }
//					// IORWFile.XmlFileCreator(ressFirstInstall);
//					// endlogo();
//				}
//			}
//		}
//		catch(FileNotFoundException e2){
//			ChannelOperation.downloadZipsError();
//		}
//		catch (Exception ex) {
//			Tools.debug(ex);
//		}
//	}

	public int unZipNum = 0; // 以解压的zip数
	public List<String> zipUnList = new ArrayList<String>(); // 待解压的包名

	/**
	 * 开线程解压zip
	 */
	public void unZipOn()
	{
		new Thread(new Runnable() {

			public void run()
			{
				try {
					int ksize = m_resZip_key.size();
					while (unZipNum < ksize) {
						// Tools.debugPrintln("以解压完的zip包数:"+unZipNum);
						if (zipUnList.size() > 0) {
							String zipKey = zipUnList.get(0);
							zipKey = zipKey.substring(zipKey.lastIndexOf("/") + 1);
							zipUnList.remove(0);
							File dalateFile;
							boolean isUnzipsucceed = IORWFile.unZip(m_zip_path
									+ zipKey, m_zip_path);
							dalateFile = new File(m_zip_path + zipKey);
							if (dalateFile != null && dalateFile.exists()) {
								dalateFile.delete();
							}
							if (isUnzipsucceed) {
								String resVersions = DWGameManager.s_config
										.getString(
												DWGameManager.CFG_KEY_RES_VERSION,
												RESOURCE_VERSION);
								int resVer = (Integer.parseInt(resVersions) + 1);
								DWGameManager.s_config
										.edit()
										.putString(
												DWGameManager.CFG_KEY_RES_VERSION,
												resVer + "").commit();
								unZipNum++;
							}
							else {
								unZipNum = ksize;
//								m_pos = Integer.MAX_VALUE; // 结束更新
//								zipIndex = m_resZip_key.size();// 结束更新
								final DWMessageBox dwBox = new DWMessageBox(
										"提示！", "游戏更新出错是否重新更新？", "确认", "退出游戏");
								dwBox.addListener(new DWListener() {
									public void OnDoubleClick()
									{
										DWControlsManager.getInstance()
												.removeControl(dwBox);
										((ResourceVerifier) DWGameManager
												.getInstance().getGameHandler())
												.setState(ResourceVerifier.LOGIC_ISResourceVersion);
									}

									public void OnClick()
									{
										DWControlsManager.getInstance()
												.removeControl(dwBox);
										DWGameManager.getInstance().destroy();
									}
								});
								DWControlsManager.getInstance().addControl(
										dwBox);
							}
						}
						else {
							Thread.sleep(100);
						}
					}
					endlogo();
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();

	}

	public void recvCheckVersion(DataInputStream dis) throws Exception
	{
		Tools.debugPrintln("recv  客户端代码版本");
		byte state = dis.readByte(); // 1： 有更新 0:的时候没更新
		if (state == 1) {
			final String url = dis.readUTF();
			Tools.debugPrintln("客户端更新地址: " + url);
			DWGameManager.getInstance().getCurrentActivity().mHandler
					.post(new Runnable() {

						public void run()
						{
							new AlertDialog.Builder(DWGameManager.getInstance()
									.getCurrentActivity())
									.setTitle("更新客户端")
									.setIcon(null)
									.setMessage("有客户端更新, 是否更新？")
									.setPositiveButton(
											"确定",
											new DialogInterface.OnClickListener() {

												public void onClick(
														DialogInterface dialog,
														int which)
												{
													DWGameManager
															.getInstance()
															.getDownloadManager()
															.startDownload(url);
													m_state = LOGIC_ISCodeVersion_Waite_Download;
												}
											})
									.setNegativeButton(
											"取消",
											new DialogInterface.OnClickListener() {

												public void onClick(
														DialogInterface dialog,
														int which)
												{
													DWGameManager.getInstance()
															.getGameHandler()
															.setRunning(false);
													DWGameManager.getInstance()
															.setRunning(false);
												}
											}).create().show();

						}
					});
		}
		else {
			m_state = LOGIC_ISResourceVersion;
		}
	}

	public void recvUpdataResEnd(DataInputStream dis)
	{
		try {
			Tools.debugPrintln("新版本更新资源结束："
					+ GameProtocol.SC_REQUEST_VERSIONS_OVER);
			// endlogo();
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}

	}

	public static void CheckVersion(final String url)
	{
		Tools.debugPrintln("客户端代码版本");
		DWGameManager.getInstance().getCurrentActivity().mHandler.post(new Runnable() {

			public void run()
			{
				new AlertDialog.Builder(DWGameManager.getInstance().getCurrentActivity()).setTitle(
						"更新客户端")
						.setIcon(null)
						.setMessage("有客户端更新是否更新？").setPositiveButton("确定",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog, int which)
									{
										DWGameManager.getInstance().getDownloadManager().startDownload(url);
										((ResourceVerifier) DWGameManager.getInstance().getGameHandler())
												.setState(LOGIC_ISCodeVersion_Waite_Download);
									}
								}).setNegativeButton("取消",
								new DialogInterface.OnClickListener() {

									public void onClick(DialogInterface dialog, int which)
									{
										DWGameManager.getInstance().getGameHandler().setRunning(false);
										DWGameManager.getInstance().setRunning(false);
									}
								}).create().show();

			}
		});
	}

	private void recvResource(DataInputStream dis)
	{
		// try {
		// if (resAll == null) {
		// resAll = new HashMap<String, ResHead>();
		// }
		// ResHead head;
		// DWSerializable inst;
		// short num = dis.readShort();
		// Tools.debugPrintln("接收缺失资源：" + GameProtocol.SC_RECEIVE_RESOURCE);
		// for (int i = 0; i < num; i++) {
		// byte a = dis.readByte();
		// String b = dis.readUTF();
		// // System.out.println("b=" + b);
		// head = new ResHead(a, b, 1);
		// int dataLength = dis.readInt();
		// byte[] data = new byte[dataLength];
		// dis.read(data);
		// // 解析数据包并且进行相应的持久化处理
		// ResourceManager.getInstance().downloadResource(head, data);
		// }
		// }
		// catch (Exception ex) {
		// Tools.debug(ex);
		// }
	}

	public void checkRes()
	{
		// IORWFile.getXml();
		// missRes = IORWFile.isMissRes(resAll);
		// if (missRes.size() == 0 || missRes == null) {
		// endlogo();
		// }
		// else {
		// DWGameManager.getInstance().requestResource(missRes);
		// }
		// if (resAll == null) {
		// resAll = new HashMap<String, ResHead>();
		// }
		// IORWFile.getXml();
		// missRes = IORWFile.isMissRes(resAll);
		// if (missRes.size() == 0 || missRes == null) {
		// endlogo();
		// }
		// else {
		// DWGameManager.getInstance().requestResource(missRes);
		// isSendMissRes = true;
		// }
	}

	public void endlogo()
	{
		if (updataVersions != null) {
			DWGameManager.s_config.edit()
					.putString(DWGameManager.CFG_KEY_RES_VERSION,
							updataVersions).commit();
			Tools.debugPrintln("当前资源版本" + updataVersions);
		}
		if (DWGameManager.s_adpater != null) {
			int value = 0;
			if (DWGameManager.AdpaterType == DWGameManager.AD_HIGH) {
				value = 1;
			}
			DWGameManager.s_adpater.edit().putInt(DWGameManager.ADAPTER_NAME,
					value).commit();
		}
		if (DWGameManager.s_halfRes != null && ISHALF_PACKAGE) {
			DWGameManager.s_halfRes.edit().putBoolean(DWGameManager.HALFRES_NAME,
					true).commit();
		}
		DWKey.createInstance();
		setState(LOGIC_IS_INITSDK);
	}

	public boolean doDown(PointF point)
	{
		return false;
	}

	public boolean doClick(PointF point)
	{
		float pointX = point.x;
		float pointY = point.y;
		if (DWControlsManager.getInstance() != null) {
			DWControlsManager.getInstance().doClick(pointX, pointY);
		}
		return true;
	}

	public boolean doDoubleClick(PointF point)
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean doLongPress(PointF point)
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean doScroll(PointF start, PointF end, float distanceX,
			float distanceY)
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean doLongScroll(PointF start, PointF end, float distanceX,
			float distanceY)
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean doFling(PointF start, PointF end, float velocityX,
			float velocityY)
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean doDrag(PointF start, PointF end, float velocityX,
			float velocityY)
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onTouch(MotionEvent event, PointF point)
	{
		if (DWControlsManager.getInstance() != null)
			return DWControlsManager.getInstance().onTouch(event, point);
		return false;
	}

	private int currentFrameIndex;

	public void paint(DWGraphics g)
	{
		int color = 0xFFF3BA0C;
		StringBuffer dotNum = new StringBuffer();
		dotNum.append(" .");
		for (int i = 1; i < (currentFrameIndex / 8); i++) {
			dotNum.append(" .");
		}
		if (currentFrameIndex == 79) {
			currentFrameIndex = 0;
		}
		g.setClip(0, 0, DWGameManager.Screen_Width, DWGameManager.Screen_Height);
		g.setColor(0xff000000);
		g.fillRect(0, 0, DWGameManager.Screen_Width,
				DWGameManager.Screen_Height);
		if (m_state != LOGIC_Logo && m_state != LOGIC_Logo2) {
			if (m_login_img != null) {
				g.drawBitmap(m_login_img, DWGameManager.Screen_Width >> 1,
						DWGameManager.Screen_Height >> 1,
						DWGameManager.Screen_Width,
						DWGameManager.Screen_Height, g.HCENTER | g.VCENTER);
			}
			if (m_login_img_max != null) {
				if (DWGameManager.AdpaterType == DWGameManager.AD_HIGH) {
					g.drawBitmap(m_login_img_max, DWGameManager.Screen_Width >> 1,
							30, m_login_img_max.getWidth() * (float) 1.5, m_login_img_max.getHeight() * (float) 1.5,
							g.HCENTER | g.TOP);
				}
				else {
					g.drawBitmap(m_login_img_max, DWGameManager.Screen_Width >> 1,
							30, g.HCENTER | g.TOP);
				}
			}
		}
		String resVersions = DWGameManager.s_config.getString(
				DWGameManager.CFG_KEY_RES_VERSION, RESOURCE_VERSION);
		g.drawShadowString("当前客户端版本： " + CLIENT_VERSION, Color.BLACK,
				color, 20, 20, Tools.LEFT_TOP);
		g.drawShadowString("当前资源版本： " + resVersions, Color.BLACK, color,
				20, (60), Tools.LEFT_TOP);
		if (m_state == LOGIC_ISFIRSTLOGIN_DEALING || m_state == m_selictSer || m_state == LOGIC_Logo
				|| m_state == LOGIC_Logo2) {
			DWGameManager.getInstance().m_disoff = false;
			if (DWGameManager.getInstance().m_img != null
					&& DWControlsManager.getInstance().contains(DWGameManager.getInstance().m_img)) {
				DWControlsManager.getInstance().removeControl(DWGameManager.getInstance().m_img);
			}
		}
		else {
			DWGameManager.getInstance().m_disoff = true;
		}
		switch (m_state) {
			case m_selictSer:
				// int h3 = DWGameManager.Screen_Height / 4;
				// g.drawString("外网", Color.WHITE, DWGameManager.Screen_Width >> 1,
				// h3 >> 1, Tools.HCENTER_BOTTOM);
				// g.drawString("内网1995", Color.WHITE, DWGameManager.Screen_Width >>
				// 1, h3 + (h3 >> 1),
				// Tools.HCENTER_BOTTOM);
				// g.drawString("内网1986", Color.WHITE, DWGameManager.Screen_Width >>
				// 1, (h3 * 2) + (h3 >> 1),
				// Tools.HCENTER_BOTTOM);
				// g.drawString("内网8888", Color.WHITE, DWGameManager.Screen_Width >>
				// 1, (h3 * 3) + (h3 >> 1),
				// Tools.HCENTER_BOTTOM);
				break;
			case LOGIC_Logo:
			case LOGIC_Logo2:
				if (m_logo_img != null) {
					g.drawBitmap(m_logo_img, DWGameManager.Screen_Width >> 1,
							DWGameManager.Screen_Height >> 1, g.HCENTER | g.VCENTER);
				}

				break;
			case LOGIC_ISFIRSTLOGIN_DEALING:
				g.drawShadowString("第一次安装处理资源中(大约一至两分钟):  " + dotNum.toString(),
						Color.BLACK, color, 20,
						(DWGameManager.Screen_Height - 80), Tools.LEFT_BOTTOM);
				break;
			case LOGIC_ISFIRSTLOGIN:
			case LOGIC_ISFIRSTLOGINCC:
				break;
			case LOGIC_Waite_Connection:
			case LOGIC_Connection:
				g.drawShadowString("联网中 ,请稍候 " + dotNum.toString(), Color.BLACK,
						color, 20, (DWGameManager.Screen_Height - 80),
						Tools.LEFT_BOTTOM);
				break;
			case LOGIC_ISHTTP:
				g.drawShadowString("请求服务器列表  " + dotNum.toString(), Color.BLACK,
						color, 20, (DWGameManager.Screen_Height - 80),
						Tools.LEFT_BOTTOM);
				break;
			case LOGIC_ISCodeVersion:
				g.drawShadowString("检查客户端是否需要更新  " + dotNum.toString(),
						Color.BLACK,
						color, 20,
						(DWGameManager.Screen_Height - 80), Tools.LEFT_BOTTOM);
				break;
			case LOGIC_ISCodeVersion_Waite_Check:
				g.drawShadowString("检查客户端是否需要更新  " + dotNum.toString(),
						Color.BLACK,
						color, 20,
						(DWGameManager.Screen_Height - 80), Tools.LEFT_BOTTOM);
				break;
			case LOGIC_ISCodeVersion_Waite_Download:
				g.drawShadowString("更新客户端中  " + UpdateManager.progress + "%",
						Color.BLACK,
						color, 20,
						(DWGameManager.Screen_Height - 80), Tools.LEFT_BOTTOM);
				break;
			case LOGIC_ISResourceVersion:
				g.drawShadowString("检查是否有资源更新  " + dotNum.toString(), Color.BLACK,
						color, 20, (DWGameManager.Screen_Height - 80),
						Tools.LEFT_BOTTOM);
				break;
			case LOGIC_ISResourceVersion_Download:
				String jingdu = "";
				String jingdu2 = "";
				if (m_resZip_key != null) {
					int zipSize = m_resZip_key.size();
					if (m_httpDownZip_index < zipSize) {
//						if (zipSize > 0) {
//							jingdu = jingdu + (zipIndex + 1) + "/" + zipSize;
//						}
//						if (currentZipsize > 0) {
//							long f = m_pos * (long) 100 / currentZipsize;
//							jingdu = jingdu + "  " + f + "%";
//						}
						if (zipSize > 0) {
							jingdu = jingdu + (m_httpDownZip_index + 1) + "/" + zipSize;
						}
						if (UpdateManager.size > 0) {
							long f = UpdateManager.dqsize * (long) 100 / UpdateManager.size;
							jingdu = jingdu + "  " + f + "%";
							jingdu2 = "   (" + UpdateManager.dqsize / 1000 + "k/" + UpdateManager.size / 1000 + "k)";
						}
					}
					else {
						jingdu2 = jingdu2 + "更新完毕,处理资源中请稍等";
					}
				}
				g.drawShadowString("更新资源中:  " + jingdu, Color.BLACK, color,
						DWGameManager.Screen_Width >> 1, (DWGameManager.Screen_Height - 80), Tools.RIGHT_BOTTOM);
				g.drawShadowString(jingdu2, Color.BLACK, 0xfff56404,
						DWGameManager.Screen_Width >> 1, (DWGameManager.Screen_Height - 80), Tools.LEFT_BOTTOM);
				break;
		}

//		if (time <= 0) {
		if (m_state >= LOGIC_ISFIRSTLOGIN) {
			DrawPrompt(g);
		}
//		} else {
//			time--;
//		}
		currentFrameIndex++;
		if (DWControlsManager.getInstance() != null)
			DWControlsManager.getInstance().paint(g);
	}

	private int time = 200;// 等待显示时间
	private float Length = 0;// 滚动长度
	private int speed = 5;// 滚动速度
	private float str_x = 0;
	private String str = null;

	public void DrawPrompt(DWGraphics g)
	{

		if (str == null) {
			str = Loading.STR_TIPS[Tools.random(Loading.STR_TIPS.length - 1)];
			Length = DWFont.getDefaultFont().stringWidth(str);
		}

		int x = DWGameManager.Screen_Width - 600 >> 1;
		int y = DWGameManager.Screen_Height - 158;
		int w = 600;
		int h = 30;

		g.setColor(0x55000000);
		g.fillRoundRect(x, y, w, h, 8, 8);

		g.setClip(x, y, w, h);
//		g.drawString(str, 0xFFF3BA0C, (x + w) - str_x,
//				y + (h - g.getFontHeight() >> 1), 0);

		g.drawShadowString(str,
				Color.BLACK,
				0xFFF3BA0C, (x + w) - str_x,
				y + (h - g.getFontHeight() >> 1), 0);

		g.setClip(0, 0, DWGameManager.Screen_Width, DWGameManager.Screen_Height);

		if (str_x < (Length + w)) {
			str_x += speed;
		}
		else {
			str_x = 0;
			str = null;

		}

	}

	@Override
	public void init()
	{
		try {
			if (m_state == m_selictSer) {
				selectServer();
			}

			if (DWGameManager.getInstance().getCurrentActivity() != null) {
				Context context = DWGameManager.getInstance().getCurrentActivity().getContext();
				int id = context.getResources().getIdentifier("logo", "raw", context.getPackageName());

				InputStream is = Tools.getResourceAndroidRes(DWGameManager
						.getInstance().getCurrentActivity(), id);
				m_logo_img = BitmapFactory.decodeStream(is);
				is.close();

				id = context.getResources().getIdentifier("denglubjt_1", "raw", context.getPackageName());
				InputStream is2 = Tools.getResourceAndroidRes(DWGameManager
						.getInstance().getCurrentActivity(), id);
				m_login_img = BitmapFactory.decodeStream(is2);
				is2.close();

				id = context.getResources().getIdentifier("denglulg_1", "raw", context.getPackageName());
				InputStream is3 = Tools.getResourceAndroidRes(DWGameManager
						.getInstance().getCurrentActivity(), id);
				m_login_img_max = BitmapFactory.decodeStream(is3);
				is3.close();
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	@Override
	public void quit()
	{
		if (m_logo_img != null && !m_logo_img.isRecycled()) {
			m_logo_img.recycle();
			m_logo_img = null;
		}
		if (m_login_img != null && !m_login_img.isRecycled()) {
			m_login_img.recycle();
			m_login_img = null;
		}
		if (m_login_img_max != null && !m_login_img_max.isRecycled()) {
			m_login_img_max.recycle();
			m_login_img_max = null;
		}
	}

	private int m_ip_index = 0;
	private int m_port_index = 0;

	public void selectServer()
	{
		final DWFrame serverFrame = new DWFrame(DWGameManager.Screen_Width,
				DWGameManager.Screen_Height);
		serverFrame.setBackgroundColor(Color.BLACK);

		final String[] serverIP = { "122.192.64.122" ,"221.130.10.137", "58.215.185.99",
				"221.130.10.142", "192.2.9.191", "192.2.8.162",
				"221.130.10.134", "221.130.10.143",
				
				};
		final int[] serverPort = { 23590,30000, 23591, 8888, 1984, 1985, 1986, 1995,
				18007, 15555, 23592, 23594, 29999};
		final DWLabel[] serverL = new DWLabel[serverIP.length];
		for (int i = 0; i < serverIP.length; i++) {
			serverL[i] = new DWLabel(serverIP[i]);
			serverL[i].setFont(DWFont.getFont(30));
			serverL[i].resetWideHigh();
			serverL[i].setNearAnchor(serverFrame, Tools.LEFT_TOP,
					Tools.LEFT_TOP, 100, 50 + i * 60);
			if (i == 0) {
				serverL[i].setTextColor(Color.YELLOW);
			}
			final int index = i;
			serverL[i].addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					serverL[m_ip_index].setTextColor(Color.WHITE);
					serverL[index].setTextColor(Color.YELLOW);
					m_ip_index = index;
				}
			});
			serverFrame.addControl(serverL[i]);
		}

		final DWLabel[] portL = new DWLabel[serverPort.length];
		for (int i = 0; i < serverPort.length; i++) {
			portL[i] = new DWLabel(Integer.toString(serverPort[i]));
			portL[i].setFont(DWFont.getFont(30));
			portL[i].resetWideHigh();
			if (i % 2 == 0) {
				portL[i].setNearAnchor(serverL[0], Tools.LEFT_TOP,
						Tools.RIGHT_TOP, 200, (i / 2) * 60);
			}
			else {
				portL[i].setNearAnchor(portL[i - 1], Tools.LEFT_TOP,
						Tools.RIGHT_TOP, 100, 0);
			}
			if (i == 0) {
				portL[i].setTextColor(Color.YELLOW);
			}
			final int index = i;
			portL[i].addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					portL[m_port_index].setTextColor(Color.WHITE);
					portL[index].setTextColor(Color.YELLOW);
					m_port_index = index;
				}
			});
			serverFrame.addControl(portL[i]);
		}

		// final DWInputBox srcV = new DWInputBox(100, 50, serverFrame);
		// srcV.setNearAnchor(serverCB[serverCB.length - 1], Tools.LEFT_TOP,
		// Tools.RIGHT_BOTTOM, 50, 50);
		// serverFrame.addControl(srcV);
		//
		// DWLabel srcN = new DWLabel("代码版本号");
		// srcN.setNearAnchor(srcV, Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER,
		// -20, 0);
		// serverFrame.addControl(srcN);
		//
		// final DWInputBox resV = new DWInputBox(100, 50, serverFrame);
		// resV.setNearAnchor(srcV, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 20);
		// serverFrame.addControl(resV);
		//
		// DWLabel resN = new DWLabel("资源版本号");
		// resN.setNearAnchor(resV, Tools.RIGHT_VCENTER, Tools.LEFT_VCENTER,
		// -20, 0);
		// serverFrame.addControl(resN);

		DWButton button = new DWButton("渠道号:" + DWChannel.CHANNEL, 200, 80);
		button.setNearAnchor(serverFrame, Tools.RIGHT_BOTTOM,
				Tools.RIGHT_BOTTOM, -10, -10);
		button.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				DWGameManager.getInstance().setConnectionServiceInfo(
						serverIP[m_ip_index], serverPort[m_port_index]);
				// DWGameManager.getInstance().createConnection(serverIP[m_ip_index],
				// serverPort[m_port_index]);
				// DWGameManager.getInstance().getConnection().open();
				DWControlsManager.getInstance().removeControl(serverFrame);
				m_state = LOGIC_Logo;
			}
		});
		serverFrame.addControl(button);

		DWControlsManager.getInstance().addControl(serverFrame);
	}

//	仙缘服务器ip：122.192.64.122
//	端口：23590
//	客户端版本号：1.0.0.0
//	客户端资源版本号：1.0.0

	/** 起动获取服务器 */
	public void initHttp()
	{
		String server = "android_HTTP/http_findServerInfo?ga.pcOrPh=1";
		//#if http_server_ip==0
		//# String host = "221.130.10.135:1111";
		//#elif http_server_ip==1
		//# String host = "221.130.10.143:1111";
		//#else
//		String host = "221.130.10.135:1111";
		//#endif
		
		String host = "122.192.64.122:23590";
		HttpConnect http = new HttpConnect(server, host);
		new Thread(http).start();
	}

	private DWConnectionCreator m_connectionCreator;

	public void openConnection()
	{
		if (m_connectionCreator == null
				&& (DWGameManager.getInstance().getConnection() == null || !DWGameManager.getInstance().getConnection()
						.isAlive())) {
			m_connectionCreator = new DWConnectionCreator();
			new Thread(m_connectionCreator).start();
		}
	}
}
